package MGK2;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Run {

	/**
	 * @param args
	 */
	@SuppressWarnings("unused")
	public static void main(String[] args) {
		
		
		System.out.println("Starting to scan...");
		int year = Calendar.getInstance().get(Calendar.YEAR);
		List<Movie> todayMovies = new ArrayList<Movie>();
		
		
		
		try {
			
//			//parse main to get all movie titles and url
//			
//			String url = "http://kino.search.ch/zurich/original";
//			Document doc = Jsoup.parse(new URL(url).openStream(), "UTF-8", url);
//		//	doc = Jsoup.connect("http://kino.search.ch/zurich/original").get();
//			//Elements newsHeadlines = doc.select(".kino_th_link");
//			Elements e = doc.select(".kino_th_link");
//			//System.out.println(e.html());
//			for(int i = 0; i < e.size(); i++)
//			{
//				todayMovies.add(new Movie(e.get(i).html(), year, e.get(i).absUrl("href")));				
//			}
//			
			
			
			//call get full data for every movie
			
			String url = "http://kino.search.ch/zurich/scary-movie-5-2013/original";
			Document doc = Jsoup.parse(new URL(url).openStream(), "UTF-8", url);
			// Elements e = doc.select(".kino_th"); //kino name
				
			Elements elementsWann = doc.getElementsByClass("wann");
			
			if (elementsWann.size() < 1)
			{
				throw new WebsiteCodeChangedException("There is no more <div class\"wann\"> anymore in " + url);
			}
			Elements elementsWannTr = elementsWann.get(0).getElementsByTag("tr");
			
			for(int i=0; i <  elementsWannTr.size(); i++)
			{
				//todo here: if first Tr then its the name, second, third, until "Homepage: www.kitag..." its the room!
				//System.out.println(elementsWannTr.get(0).html());
				
			}
		
			
			
				
			
			
//			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("hello.txt"), "UTF-8"));
//			try{
//				for(int i = 0; i < e.size(); i++)
//				{
//					out.write(todayMovies.get(i).title);
//					out.write("\n");
//				}
//			} finally {
//				    out.close();
//			}
				
				
				
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WebsiteCodeChangedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("Scanning done!");	

	}

}
